package it.infodea.tapestrydea.components.jcr;

import it.infodea.tapestrydea.services.jcr.JcrWorkspaceService;
import it.infodea.tapestrydea.services.jcr.nodes.wrappers.JcrFileNode;

import javax.jcr.ItemNotFoundException;
import javax.jcr.RepositoryException;

import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

public class LinkJcr {

	@Parameter(required = true)
	private String uuid;

	@Parameter
	private String label;

	@Inject
	private JcrWorkspaceService jcrService;

	@Inject
	private Request request;

	private JcrFileNode fileNode;

	private String link;

	@BeginRender
	void init() throws ItemNotFoundException, RepositoryException {
		try {
			fileNode = jcrService.getFileByUUID(uuid);
			if (fileNode != null) {
				if (label == null) {
					label = fileNode.getName();
				}
				link = fileNode.toClientURL(request);
			}
		} catch (ItemNotFoundException e) {
		}
	}

	public String getLink() {
		return link;
	}

	public String getLabel() {
		return label;
	}

	public JcrFileNode getFileNode() {
		return fileNode;
	}

}
